<?php

/**
 * This is the model class for table "profesores".
 *
 * The followings are the available columns in table 'profesores':
 * @property integer $id
 * @property string $codigo
 *
 * The followings are the available model relations:
 * @property Solicitudescambiohorario[] $solicitudescambiohorarios
 * @property Marcaciones[] $marcaciones
 * @property Liquidaciones[] $liquidaciones
 * @property Solicitudescambiofecha[] $solicitudescambiofechas
 * @property Asistencias[] $asistenciases
 * @property Clases[] $clases
 * @property Categorias[] $categoriases
 * @property Users $id0
 */
class Profesores extends CActiveRecord
{
	private $fullname;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'profesores';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id', 'required'),
			array('id', 'numerical', 'integerOnly'=>true),
			array('codigo', 'length', 'max'=>100),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, fullname, codigo', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'solicitudescambiohorarios' => array(self::HAS_MANY, 'Solicitudescambiohorario', 'id'),
			'marcaciones' => array(self::HAS_MANY, 'Marcaciones', 'id'),
			'liquidaciones' => array(self::HAS_MANY, 'Liquidaciones', 'id'),
			'solicitudescambiofechas' => array(self::HAS_MANY, 'Solicitudescambiofecha', 'id'),
			'asistenciases' => array(self::HAS_MANY, 'Asistencias', 'id'),
			'clases' => array(self::HAS_MANY, 'Clases', 'id'),
			'categoriases' => array(self::HAS_MANY, 'Categorias', 'id'),
			'id0' => array(self::BELONGS_TO, 'User', 'id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Usuario',
			'fullname' => 'Profesor',
			'codigo' => 'Código',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->with = array('id0.profile');
		$criteria->compare('id',$this->id);
		$criteria->compare('codigo',$this->codigo,true);
		$criteria->addSearchCondition('CONCAT(profile.lastname,", ",profile.firstname)',$this->fullname,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
				'defaultOrder'=>'"profile".lastname ASC, "profile".firstname ASC',
				'attributes'=>array('*', "fullname"=>array(
					'asc'=>'"profile".lastname ASC, "profile".firstname ASC',
					'desc'=>'"profile".lastname DESC, "profile".firstname DESC',
				)),
			),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Profesores the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	protected function afterSave()
	{
		parent::afterSave();
		Rights::assign('Profesor',$this->id);
	}

	protected function beforeDelete()
	{
		if (parent::beforeDelete() == false) return false;
		Rights::revoke('Profesor',$this->id);
		return true;
	}

	public function getDisplay()
	{
		return $this->id0->profile->fullname;
	}

	public function getFullname()
	{
		return $this->id0->profile->fullname;
	}
}
